package com.droidski.taskdb.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.droidski.taskdb.TaskDB;
import com.droidski.taskdb.Util;
import com.droidski.taskdb.model.Task;

public class RestHandler extends BaseRestHandler {

	TaskDB taskDB;
	
	public RestHandler(TaskDB taskDB) {
		super();
		this.taskDB = taskDB;
	}

	@Rest(name="list",
			regex="/list.*")
	public void ls(HttpServletRequest request, HttpServletResponse res) throws ServletException, IOException {
		
		Task root = taskDB.getCurrentRootTask();
		switch ( getOutputFormat(request) ) {
		case JSON:
			listJSON(root, 0, res);
			break;
		case PLAINTEXT:
		case XML:
			listXML(res);
			break;
		}
		
	}
	
	private void listXML(HttpServletResponse response) throws IOException {

		PrintWriter out = response.getWriter();
		
		out.println("<?xml version='1.0' encoding='utf-8'?>");
		out.println("<tasks root='" + taskDB.getCurrentRootTask().getId() + "'>");
		for ( Task task : taskDB.getTasks().values() ) {
				
			out.println("<task>");
			out.println("<id>"+task.getId()+"</id>");
			out.println("<name>" + task.getName() + "</name>");
			out.println("<parent_id>" + task.getParentId() + "</parent_id>");
			out.println("<description>" + task.getDescription() + "</description>");
			out.println("<alias>" + task.getAlias()+"</alias>");
			if ( task.getChildren().size() > 0 ) {
				out.println("<children>");
				for ( int index=0; index<task.getChildren().size(); index++) {
					String childId = task.getChildren().get(index);
					out.println("<child>" + childId + "</child>");
				}
				out.println("</children>");
			}
			out.println("</task>");
			
		}
		out.println("</tasks>");		
		
	}

	private void listJSON(Task task, int depth, HttpServletResponse response) throws IOException {

		PrintWriter out = response.getWriter();
		String tabs = "";
		if ( depth > 0 ) {
			tabs = Util.repeat("\t", depth+1);
		}
		String tabs2 = Util.repeat("\t", depth+1);
		String tabs3 = Util.repeat("\t", depth+2);
			
		out.print("\n" + tabs + "{\n");
		out.print(tabs+"'id':'" + task.getId() + "',\n");
		out.print(tabs+"'name':'" + task.getName() + "',\n");
		out.print(tabs+"'parent_id':'" + task.getParentId() + "',\n");
		out.print(tabs+"'description': '" + task.getDescription() + "',\n");
		out.print(tabs+"'alias':'" + task.getAlias()+"',\n");
		if ( task.getChildren().size() > 0 ) {
			out.print(tabs+"'children': [");
			for ( int index=0; index<task.getChildren().size(); index++) {
				String childId = task.getChildren().get(index);
				Task child = taskDB.findTaskById(childId);
				listJSON(child, depth+1, response);
				if ( index+1<task.getChildren().size()) {
					out.print(tabs3+",");
				}
			}
			out.print("\n" + tabs2 +"]\n");
		} else {
			out.print(tabs+"'children':[],\n");
		}
				
		out.print(tabs + "}\n");
		
	}

	@Rest(name="add",
			regex="/add.*")
	public void add(HttpServletRequest request, HttpServletResponse res) throws ServletException, IOException {
		res.getWriter().println("add");
	}

	@Rest(name="remove",
			regex="/remove.*")
	public void remove(HttpServletRequest request, HttpServletResponse res) throws ServletException, IOException {
		res.getWriter().println("remove");
	}

	@Rest(name="search",
			regex="/search.*")
	public void search(HttpServletRequest request, HttpServletResponse res) throws ServletException, IOException {
		res.getWriter().println("ls");
	}

	@Rest(name="move",
			regex="/move.*")
	public void update(HttpServletRequest request, HttpServletResponse res) throws ServletException, IOException {
		res.getWriter().println("move");
	}

	@Rest(name="start",
			regex="/start.*")
	public void start(HttpServletRequest request, HttpServletResponse res) throws ServletException, IOException {
		res.getWriter().println("start");
	}

	@Rest(name="pause",
			regex="/pause.*")
	public void pause(HttpServletRequest request, HttpServletResponse res) throws ServletException, IOException {
		res.getWriter().println("pause");
	}

	@Rest(name="complete",
			regex="/complete.*")
	public void complete(HttpServletRequest request, HttpServletResponse res) throws ServletException, IOException {
		res.getWriter().println("complete");
	}

	@Rest(name="status",
			regex="/status.*")
	public void status(HttpServletRequest request, HttpServletResponse res) throws ServletException, IOException {
		res.getWriter().println("status");
	}

	@Rest(name="history",
			regex="/history.*")
	public void history(HttpServletRequest request, HttpServletResponse res) throws ServletException, IOException {
		res.getWriter().println("history");
	}
	
	@Rest(name="report",
			regex="/report.*")
	public void report(HttpServletRequest request, HttpServletResponse res) throws ServletException, IOException {
		res.getWriter().println("report");
	}
	
	@Rest(name="priority",
			regex="/priority.*")
	public void priority(HttpServletRequest request, HttpServletResponse res) throws ServletException, IOException {
		res.getWriter().println("priority");
	}

	@Rest(name="tag",
			regex="/tag.*")
	public void tag(HttpServletRequest request, HttpServletResponse res) throws ServletException, IOException {
		res.getWriter().println("tag");
	}
	
	@Rest(name="move",
			regex="/move.*")
	public void move(HttpServletRequest request, HttpServletResponse res) throws ServletException, IOException {
		res.getWriter().println("move");
	}
	
	@Rest(name="rename",
			regex="/rename.*")
	public void rename(HttpServletRequest request, HttpServletResponse res) throws ServletException, IOException {
		res.getWriter().println("rename");
	}


	@Rest(name="category",
			regex="/category.*")
	public void category(HttpServletRequest request, HttpServletResponse res) throws ServletException, IOException {
		res.getWriter().println("category");
	}
	
	@Rest(name="project",
			regex="/project.*")
	public void project(HttpServletRequest request, HttpServletResponse res) throws ServletException, IOException {
		res.getWriter().println("project");
	}

	
}
